'use strict';
const fs = require('fs');
const uuid = require('uuid');
const BaseController = require('../../core/base_controller');

class UploadController extends BaseController {
    async uploadToQiNiu() {
        const {ctx} = this
        const files = ctx.request.files
        const resFiles = []
        for(let file of files){
          const stream =   fs.createReadStream(file.filepath)
            const etc = file.filename.split('.')[1]
            const filename = uuid.v4().replaceAll('-','') + '.' + etc
            let r = await this.service.qiniu.uploadStream(stream,filename);
            r.url = r.url.includes('http')? r.url : 'http://'+ r.url
            resFiles.push(r)
        }
        this.success(resFiles);
    }
}

module.exports = UploadController;
